package com.winit.tree.web;

import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import com.winit.flow.web.FlowController;
import com.winit.tree.manager.TreeManager;
import com.winit.tree.model.TreeNode;
import com.winit.tree.model.TreeType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(value = "flowFrame", tags = {"flowFrame"}, description = "flowFrame")
@RestController
@RequestMapping("/api/v1/flowFrame")
public class FlowFrameController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(FlowFrameController.class);

    @Autowired
    private TreeManager treeManager;

    @RequestMapping(value = "/getFlowFrameTree", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "FlowFrame tree")
    @ResponseBody
    public Map getFlowFrameTree(@ApiParam(value = "flowFrameId, 根id，缺省默认查整棵树") @RequestParam(value = "flowFrameId", required = false) String flowFrameId) {
        TreeNode flowFrame = this.treeManager.getTree(flowFrameId, TreeType.FLOW_FRAME);
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, flowFrame);
    }

    @RequestMapping(value = "/getTreeNode", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取 tree node")
    @ResponseBody
    public Map getTreeNode(@ApiParam(value = "flowFrameId",required = true) @RequestParam(value = "flowFrameId", required = true) String flowFrameId) {
        FlowController.FlowTreeNode flowTreeNode = this.treeManager.getTreeNode(flowFrameId);
        return this.getRtn("0", "success", flowTreeNode);
    }

    @RequestMapping(value = "/saveFlowFrame", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "创建/更新Flow frame")
    @ResponseBody
    public Map saveFlowFrame(@RequestBody TreeNode treeNode) {
        ExecuteResult<TreeNode> rs = null;
        if (StringUtils.isEmpty(treeNode.getId())) {
            log.info("未找到flowFrameId,将执行创建");
            if (StringUtils.isEmpty(treeNode.getType())) {
                treeNode.setType(TreeType.FLOW_FRAME);
            }
            rs = this.treeManager.createTreeNode(treeNode);
        } else {
            log.info("flowFrameId存在，将执行更新");
            rs = this.treeManager.updateTreeNode(treeNode);
        }

        Object data = rs.getData() == null ? null : rs.getData().getId();
        return this.getRtn(rs.getCode(), rs.getDesc(), data);
    }


    @RequestMapping(value = "/deleteFlowFrame", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "删除FlowFrame")
    @ResponseBody
    public Map deleteFlowFrame(@ApiParam(value = "flowFrameId", required = true) @RequestParam(value = "flowFrameId", required = true) String flowFrameId) {
        ExecuteResult executeResult = this.treeManager.deleteTreeNodeById(flowFrameId);
        return this.getRtn(executeResult.getCode(), executeResult.getDesc());
    }

    @RequestMapping(value = "/childSort", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存子节点顺序")
    @ResponseBody
    public Map saveChildSort(@ApiParam(value = "flowFrameId", required = true) @RequestParam(value = "flowFrameId", required = true) String flowFrameId,
                             @ApiParam(value = "sort", required = true) @RequestParam(value = "sort", required = true) String sort) {
        this.treeManager.saveChildSort(flowFrameId, sort);
        return this.getRtn(REQUEST_SUCCESS, "");
    }

}
